package com.zhiqiang.gameserver.main;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.zhiqiang.gameserver.constant.R;
import com.zhiqiang.gameserver.netty.NettyGameServer;
import com.zhiqiang.gameserver.orm.dao.Automax;
import com.zhiqiang.gameserver.orm.dao._MappingKit;
import com.zhiqiang.gameserver.queue.CommonQueue;
import com.zhiqiang.gameserver.thread.HandleMsgThread;
import com.zhiqiang.gameserver.util.JedisUtil;
import redis.clients.jedis.Jedis;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServerStartUp {
    public static void main(String[] args) throws Exception {
        initOrm();//数据库初始化
        initRedis();//redis 初始化
        ExecutorService exec = Executors.newCachedThreadPool();
        exec.execute(new HandleMsgThread(CommonQueue.getInstance()));
        exec.execute(new NettyGameServer(8888));

    }

    private static void initOrm(){
        DruidPlugin druidPlugin = new DruidPlugin(R.OrmPara.MYSQL_JDBC_URL,R.OrmPara.MYSQL_JDBC_USER,R.OrmPara.MYSQL_JDBC_PWD);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        _MappingKit.mapping(arp);
        arp.setDialect(new MysqlDialect());
        druidPlugin.start();
        arp.start();
    }

    private static void initRedis(){
        Automax autoMax = new Automax().dao();
        Automax am = autoMax.findById(1);
        Jedis jedis= JedisUtil.getInstance().getJedis();
        jedis.set(R.OrmPara.MAXUSERIDSTRING,am.getMAXUserId().toString());
        jedis.set(R.OrmPara.MAXACCOUNTIDSTRING,am.getMAXAccountId().toString());
        JedisUtil.getInstance().returnJedis(jedis);
    }
}
